package com.gdust.hzx.user.controller;

import com.gdust.hzx.user.service.UserInfoService;
import com.gdust.hzx.model.dto.h5.UserLoginDto;
import com.gdust.hzx.model.dto.h5.UserRegisterDto;
import com.gdust.hzx.model.vo.common.Result;
import com.gdust.hzx.model.vo.common.ResultCodeEnum;
import com.gdust.hzx.model.vo.h5.UserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName: UserInfoController
 * Package: com.gdust.hzx.user.controller
 * Description:会员用户接口控制层
 *
 * 思路流程：先注册，再登录，最后获取当前用户信息
 *
 * @Author syun
 * @Create 2024/12/26 23:09
 * @Version 1.0
 */
@Tag(name = "会员用户接口")
@RestController
@RequestMapping("/api/user/userInfo")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 会员注册接口
     * @param userRegisterDto 注册对象条件
     * @return
     */
    @Operation(summary = "会员注册")
    @PostMapping("register")
    public Result register(@RequestBody UserRegisterDto userRegisterDto){
        //自定义方法：会员注册
        userInfoService.register(userRegisterDto);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }


    /**
     * 会员登录接口
     * @param userLoginDto 用户登录请求参数
     * @return
     */
    @Operation(summary = "会员登录")
    @PostMapping("login")
    public Result login(@RequestBody UserLoginDto userLoginDto){
        //自定义方法：会员登录
       String token  = userInfoService.login(userLoginDto);
        return Result.build(token,ResultCodeEnum.SUCCESS);
    }


    /**
     * 获取当前用户信息
     * @param request
     * @return
     */
    @Operation(summary = "获取当前用户信息")
    @GetMapping("auth/getCurrentUserInfo")
    public Result<UserInfoVo> getCurrentUserInfo(HttpServletRequest request){
        String token = request.getHeader("token");
        //自定义方法：获取当前用户信息
        UserInfoVo userInfoVo = userInfoService.getCurrentUserInfo(token);
        return Result.build(userInfoVo, ResultCodeEnum.SUCCESS);
    }


}
